package se.webbzon.oschi01.item;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ItemSetReader {
	
	private final ItemSet set = new ItemSet();
	
	public ItemSet getItemSet() {
		return set;
	}
	
	public int read(InputStream is) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		int entries = 0;
		String str = reader.readLine();
		while (str != null) {
			String[] parts = str.split(",");
			if (parts.length >= 5) try {
				final int id = Integer.parseInt(parts[0]);
				final ItemType type = ItemType.valueOf(parts[1].toUpperCase());
				final String mod = parts[2];
				final String unlocalisedName = parts[3];
				final String className = parts[4];
				set.add(new DefaultItemEntry(id,type,mod,unlocalisedName,className)); 
				entries++;
			} catch (IllegalArgumentException | NullPointerException e) {
				// Do nothing
			}
			str = reader.readLine();
		}
		return entries;
	}

}
